package com.example.mall.web.controller;

import com.example.mall.domain.SeckillProducts;
import com.example.mall.dto.SeckillProductsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SeckillProductsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ISeckillProductsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/seckillProducts")
public class SeckillProductsController {

    @Autowired
    public ISeckillProductsService seckillProductsService;

    /**
     * 分页查询秒杀商品表信息
     * @param seckillProductsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询秒杀商品表信息",notes = "分页查询秒杀商品表信息",httpMethod = "GET")
    public JSONResult queryPage(SeckillProductsQuery seckillProductsQuery){
        PageDTO<SeckillProductsDto> seckillProductsDtoPageDTO = seckillProductsService.queryPage(seckillProductsQuery);
        return JSONResult.success("查询成功",seckillProductsDtoPageDTO);
    }

    /**
     * 根据id查询秒杀商品表信息
     * @param seckillId
     * @return
     */
    @GetMapping("query/{seckillId}")
    @ApiOperation(value = "根据id查询秒杀商品表信息",notes = "根据id查询秒杀商品表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("seckillId")Long seckillId){
        //1.根据id查询用户信息
        if (seckillId == null){
            throw new GlobalCustomerException(ErrorEnumCode.SECKILLID_NOT_ID);
        }
        SeckillProducts seckillProducts = seckillProductsService.getById(seckillId);
        return JSONResult.success("查询成功",seckillProducts);
    }

    /**
     * 根据id查询秒杀商品表信息
     * @param seckillId
     * @return
     */
    @GetMapping("querySeckillProductsById/{seckillId}")
    @ApiOperation(value = "根据id查询秒杀商品表信息",notes = "根据id查询秒杀商品表信息",httpMethod = "GET")
    public JSONResult querySeckillProductsById(@PathVariable("seckillId")Long seckillId){
        //1.根据id查询用户信息
        if (seckillId == null){
            throw new GlobalCustomerException(ErrorEnumCode.SECKILLID_NOT_ID);
        }
        SeckillProducts seckillProducts = seckillProductsService.querySeckillProductsById(seckillId);
        return JSONResult.success("查询成功",seckillProducts);
    }

    /**
     * 查询全部秒杀商品表信息
     * @param
     * @return
     */
    @GetMapping("querySeckillProductsList")
    @ApiOperation(value = "查询全部秒杀商品表信息",notes = "查询全部秒杀商品表信息",httpMethod = "GET")
    public JSONResult querySeckillProductsList(){
        List<SeckillProducts> seckillProducts = seckillProductsService.querySeckillProductsList();
        return JSONResult.success("查询成功",seckillProducts);
    }

    /**
     * 新增秒杀商品表
     * @param seckillProductsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增秒杀商品表",notes = "新增秒杀商品表",httpMethod = "POST")
    public JSONResult insert(@RequestBody SeckillProductsDto seckillProductsDto){
        seckillProductsService.insert(seckillProductsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改秒杀商品表信息
     * @param seckillProductsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改秒杀商品表信息",notes = "修改秒杀商品表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody SeckillProductsDto seckillProductsDto){
        seckillProductsService.update(seckillProductsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除秒杀商品表信息
     * @param seckillId
     * @return
     */
    @PostMapping("delete/{seckillId}")
    @ApiOperation(value = "删除秒杀商品表信息",notes = "删除秒杀商品表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("seckillId") Long seckillId) {
        seckillProductsService.delete(seckillId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除秒杀商品表信息
     * @param seckillIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除秒杀商品表信息",notes = "批量删除秒杀商品表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> seckillIds) {
        seckillProductsService.deleteBatch(seckillIds);
        return JSONResult.success("批量删除成功");
    }
}
